package client.comm.baoding.ui

import android.content.Intent
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.page.RechargeOrderPageAdapter
import client.comm.baoding.api.bean.BankBind
import client.comm.baoding.databinding.ActivityMeorder2Binding
import client.comm.baoding.ui.vm.MeOrderViewModel
import client.comm.commlib.toast.toast

class RechargeOrderActivity : BaseMiddleActivity<MeOrderViewModel, ActivityMeorder2Binding>() {

    override fun getLayout(): Int {
        return R.layout.activity_meorder2
    }

    val adapter: RechargeOrderPageAdapter by lazy {
        RechargeOrderPageAdapter(supportFragmentManager)
    }

    override fun initViewModel(): MeOrderViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.viewPager.adapter = adapter
        binding.tabLayout.setupWithViewPager(binding.viewPager)

//        when (intent.getIntExtra("index", 0)) {
//            0 -> {
////                binding.tabWrap.getChildAt(0).isSelected = true
//            }
//            1 -> {
//                binding.viewPager.postDelayed({
//                    binding.viewPager.setCurrentItem(1, false)
//                }, 10)
//            }
//        }
    }

    override fun observerUI() {
        viewModel.cancleCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("取消成功")
                    viewModel.pageOrderZero.value = 0
                    viewModel.holdOrderZero()
                    viewModel.pageOne.value = 0
                    viewModel.holdOrderOne()
                }
            }
        })

        viewModel.rechargeCall2.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    viewModel.pageOrderZero.value = 0
                    viewModel.holdOrderZero()
                    viewModel.pageOne.value = 0
                    viewModel.holdOrderOne()

                    it.data?.let {
                        it.pay_url?.let {
                            val intent =
                                Intent(this@RechargeOrderActivity, WebPayActivity::class.java)
                                    .apply {
                                        putExtra("adInfo", it)
                                        putExtra("title", "支付")
                                    }
                            startActivity(intent)
                            finish()
                        } ?: { toast("pay_url null") }
                    }

                }
            }
        })

        viewModel.rechargeCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    viewModel.pageOrderZero.value = 0
                    viewModel.holdOrderZero()
                    viewModel.pageOne.value = 0
                    viewModel.holdOrderOne()

                    it.data?.let {
                        startActivity(Intent(this, Bank3CodeActivity::class.java).apply {
                            putExtra("bank_bind", it)
                            putExtra("status", "2")
                        })
                    }
                    finish()

//                    var paymentUrl = it.data?.paymentUrl
//                    var paymentId = it.data?.payment_id
//
//                    if (paymentId.isNullOrEmpty()) {
//                        toast("payment_id参数空")
//                    } else {
//                        paymentId?.let {
//                            startActivity(
//                                Intent(
//                                    this@RechargeOrderActivity,
//                                    Bank3CodeActivity::class.java
//                                ).apply {
//                                    putExtra("payment_id", it)
//                                })
//                            finish()
//                        }
//                    }

//                    SelectPaytypeDialog2(this).apply {
//                        one = { it, txt ->
//                            if (it == 1) {
//                                startActivity(Intent(this@RechargeOrderActivity, RechargeActivity::class.java))
//
//                            } else if (it == 3) {
//                                if (paymentId.isNullOrEmpty()) {
//                                    toast("payment_id参数空")
//                                } else {
//                                    paymentId?.let {
//                                        startActivity(
//                                            Intent(
//                                                this@RechargeOrderActivity,
//                                                Bank3CodeActivity::class.java
//                                            ).apply {
//                                                putExtra("payment_id", it)
//                                            })
//                                        finish()
//                                    }
//                                }
//                            } else {
//                                val paymentUrl1 = paymentUrl
//                                paymentUrl1?.let {
//                                    val intent =
//                                        Intent(this@RechargeOrderActivity, WebPayActivity::class.java)
//                                            .apply {
//                                                putExtra("adInfo", it)
//                                                putExtra("title", "支付")
//                                            }
//                                    startActivity(intent)
//                                }
//                            }
//                            finish()
//                        }
//                    }.show()

                }
            }
        })
    }

}